package com.kkwrite.regimen.provider.stream.service;

import com.kkwrite.regimen.provider.exception.ServiceException;
import com.kkwrite.regimen.provider.service.item.ItemService;
import com.kkwrite.regimen.provider.stream.StreamInputAndOutput;
import com.kkwrite.regimen.provider.stream.dto.IncreaseReviewCountDTO;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * Created by Ron on 2018/9/10 19:20.
 *
 * @author Ron
 */
@Service
public class IncreaseReviewCountService {

    @Resource
    private StreamInputAndOutput streamInputAndOutput;
    @Resource
    private ItemService itemService;

    public void send(IncreaseReviewCountDTO countDTO) {
        Assert.notNull(countDTO, "入参不能为null");
        if (!streamInputAndOutput.increaseReviewCountOutput().send(MessageBuilder.withPayload(countDTO).build())) {
            throw new ServiceException("发送评论量提升消息异常！");
        }
    }

    @StreamListener(StreamInputAndOutput.INCREASE_REVIEW_COUNT_IN_CHANNEL)
    public void handler(IncreaseReviewCountDTO countDTO) {
        itemService.reviewCountPlus(countDTO.getItemId(), countDTO.getCount());
    }

}
